using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using umbraco.interfaces;
using Commerce.Common;


namespace CommerceForUmbraco.macroControls
{
    public class CategoryPickerMacro : System.Web.UI.WebControls.DropDownList, umbraco.interfaces.IMacroGuiRendering
    {
        public string _value = "";

        #region IMacroGuiRendering Members

        public bool ShowCaption
        {
            get { return true; }
        }

        public string Value
        {
            get
            {
                string retVal = "";
                foreach (System.Web.UI.WebControls.ListItem i in base.Items)
                    if (i.Selected)
                    {
                        retVal = i.Value;
                    }
                     
                return retVal;
            }
            set
            {
                _value = value;    
            }
        }

        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            // initial value
            this.Items.Add(new System.Web.UI.WebControls.ListItem("", ""));

            // get collection values
            Commerce.Common.CategoryCollection categories = null;

            categories = new CategoryCollection().Load();

            foreach (Category thisCat in categories)
            {
                System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem();
                item.Text = thisCat.CategoryName;
                item.Value = thisCat.CategoryID.ToString();
                this.Items.Add(item);
            }
        }
    }
}
